package com.xuexiang.xuidemo.page.text.supertextview;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;

import com.xuexiang.xui_lib.component.layout.ExpandableLayout;
import com.xuexiang.xui_lib.component.textview.badge.Badge;
import com.xuexiang.xui_lib.component.textview.badge.BadgeView;
import com.xuexiang.xui_lib.component.textview.supertextview.SuperTextView;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.page.base.BasePageAbilitySlice;
import com.xuexiang.xuidemo.page.base.Page;
import com.xuexiang.xuidemo.page.text.TextAbility;
import com.xuexiang.xuidemo.util.XToastUtils;

/**
 * 超级按钮  实现shape所有的属性
 *
 * @since 2021-04-06
 */
@Page(name = "SuperTextView点击事件", action = "SuperClickAbilitySlice", ability = TextAbility.class)
public class SuperClickAbilitySlice extends BasePageAbilitySlice {
    private static final int INT_3 = 3;
    private static final int INT_9 = 9;
    private SuperTextView superTextView;
    private SuperTextView superTextViewCb;
    private SuperTextView superTextViewSwitch;
    private SuperTextView stvMessage;
    private SuperTextView stvExpandable;
    private SuperTextView stvName;
    private SuperTextView stvPhone;
    private ExpandableLayout mExpandableLayout;

    private Badge mBadge;

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_text_super_click;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        mExpandableLayout = findCastComponentById(ResourceTable.Id_expandable_layout);
        superTextView = findCastComponentById(ResourceTable.Id_super_tv);
        superTextViewCb = findCastComponentById(ResourceTable.Id_super_cb_tv);
        superTextViewSwitch = findCastComponentById(ResourceTable.Id_super_switch_tv);
        stvMessage = findCastComponentById(ResourceTable.Id_super_message_tv);
        stvExpandable = findCastComponentById(ResourceTable.Id_stv_expandable);
        stvName = findCastComponentById(ResourceTable.Id_stv_name);
        stvPhone = findCastComponentById(ResourceTable.Id_stv_phone);

        stvMessage.setRightString("      ");
        Text rightTextView = stvMessage.getRightTextView();
        mBadge = new BadgeView(getContext()).bindTarget(rightTextView)
            .setBadgeGravity(LayoutAlignment.END | LayoutAlignment.VERTICAL_CENTER)
            .setBadgePadding(INT_3)
            .setBadgeTextSize(INT_9)
            .setBadgeNumber(INT_3);
        initListeners();
    }

    private void initListeners() {
        superTextView.setOnSuperTextViewClickListener(
            superText -> XToastUtils.toast("整个item的点击事件"))
            .setLeftTopTvClickListener(textView -> XToastUtils.toast(superTextView.getLeftTopString()))
            .setLeftTvClickListener(textView -> XToastUtils.toast(superTextView.getLeftString()))
            .setLeftBottomTvClickListener(textView -> XToastUtils.toast(superTextView.getLeftBottomString()))
            .setCenterTopTvClickListener(textView -> XToastUtils.toast(superTextView.getCenterTopString()))
            .setCenterTvClickListener(textView -> XToastUtils.toast(superTextView.getCenterString()))
            .setCenterBottomTvClickListener(textView -> XToastUtils.toast(superTextView.getCenterBottomString()))
            .setRightTopTvClickListener(textView -> XToastUtils.toast(superTextView.getRightTopString()))
            .setRightTvClickListener(textView -> XToastUtils.toast(superTextView.getRightString()))
            .setRightBottomTvClickListener(textView -> XToastUtils.toast(superTextView.getRightBottomString()))
            .setLeftImageViewClickListener(imageView -> {
            })
            .setRightImageViewClickListener(imageView -> {
            });

        superTextViewCb.setOnSuperTextViewClickListener(
            superText -> superText.setCheckBoxChecked(!superText.getCheckBoxIsChecked()))
            .setCheckBoxCheckedChangeListener((buttonView, isChecked) -> XToastUtils.toast("isChecked : " + isChecked));

        superTextViewSwitch.setOnSuperTextViewClickListener(
            superText -> superText.setSwitchIsChecked(!superText.getSwitchIsChecked(), false))
            .setSwitchCheckedChangeListener((buttonView, isChecked) -> XToastUtils.toast("isChecked : " + isChecked));

        stvMessage.setOnSuperTextViewClickListener(superText -> mBadge.hide(true));
        stvName.setCenterEditTextFocusChangeListener((v, hasFocus) -> XToastUtils.toast("聚焦变化：" + hasFocus));
        stvPhone.setCenterEditTextClickListener(v -> XToastUtils.toast("点击监听"));
        mExpandableLayout.collapse(false);
        mExpandableLayout.setOnExpansionChangedListener((expansion, state) -> {
            if (stvExpandable != null && stvExpandable.getRightIconIV() != null) {
                stvExpandable.getRightIconIV().setRotation(expansion * 90);
            }
        });
        stvExpandable.setOnSuperTextViewClickListener(superText -> {
            if (mExpandableLayout != null) {
                mExpandableLayout.toggle();
            }
        });
    }
}
